import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_cupertino_date_picker/flutter_cupertino_date_picker.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_easyrefresh/material_footer.dart';
import 'package:flutter_easyrefresh/material_header.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:flutter_picker/Picker.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:panda/base/SignType.dart';
import 'package:panda/base/application.dart';
import 'package:panda/entity/common_entity.dart';
import 'package:panda/entity/order_list_entity.dart';
import 'package:panda/entity/order_list_entity3.dart';
import 'package:panda/home/MyBottomBar2.dart';
import 'package:panda/http/Api.dart';
import 'package:panda/http/HttpUtil.dart';
import 'package:panda/jiayouzhan/KefuTalk.dart';
import 'package:panda/login/Welcome.dart';
import 'package:panda/order/MyHomePage.dart';
import 'package:panda/order/OrderDetailPage.dart';
import 'package:panda/order/OrderPage2.dart';
import 'package:panda/util/AppSaveInfoUtil.dart';
import 'package:panda/util/DateUtils.dart';
import 'package:panda/util/SignUtil.dart';

import '../base/Constant.dart';
import 'OrderPay.dart';

class AllOrderVipPage extends StatefulWidget {
  var type;
  AllOrderVipPage({Key key, @required this.type}) : super(key: key);
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return AllOrderView();
  }
}

class AllOrderView extends State<AllOrderVipPage>
    with AutomaticKeepAliveClientMixin, WidgetsBindingObserver {
  List<OrderList3DataData> dataList = List();
  List<CommonEntityDataData> logs = new List();
  int page = 1;
  EasyRefreshController _controller = new EasyRefreshController();
  String token = "";
  var PickerData = [];
  String bank_id = "", name = "选择快递", time1 = "开始日期", time2 = "结束日期";
  String backReason = "", backReason2 = "", danhao = "", order_num = "";
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
    var today = DateTime.now();
    // time1 = DateUtils.instance.getFormartData(
    //     timeSamp: today.millisecondsSinceEpoch, format: "yyyy-MM-dd");
    // time2 = time1;
    getString(TOKEN).then((value) {
      token = value;
      if (token == null) {
        return;
      }
      loadList();
    });
    loadListLog();
  }

  loadListLog() async {
    Map map = new Map();
    Response<String> response =
        await HttpUtil().post(Api.GET_LOGINS, data: getSign(map));
    Map userMap = json.decode(response.data);
    if (userMap['code'] != 1) {
      Fluttertoast.showToast(msg: userMap['msg']);
      return;
    }
    CommonEntityEntity homeListEntity =
        new CommonEntityEntity.fromJson(userMap);
    if (mounted)
      setState(() {
        logs.addAll(homeListEntity.data.datas);
        for (int i = 0; i < logs.length; i++) {
          PickerData.add("\"" + logs[i].title + "\"");
        }
      });
  }

  var myState1;
  showCate() {
    new Picker(
        adapter: PickerDataAdapter<String>(
            pickerdata: new JsonDecoder().convert(PickerData.toString())),
        hideHeader: true,
        title: new Text("选择快递"),
        cancelText: "取消",
        cancelTextStyle: TextStyle(color: Colors.grey),
        confirmText: "确定",
        confirmTextStyle: TextStyle(color: mainColor),
        onConfirm: (Picker picker, List value) {
          bank_id = logs[value[0]].id;
          // controller.text = name;
          name = picker.getSelectedValues()[0];
          myState1(() {});
        }).showDialog(context);
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    // TODO: implement didChangeAppLifecycleState
    super.didChangeAppLifecycleState(state);
    if (state == AppLifecycleState.resumed)
      getString(TOKEN).then((value) {
        token = value;
      });
  }

  loadList() async {
    Map map = new Map();
    map['token'] = token;
    map['status'] = widget.type;
    if (!time1.contains("开始")) {
      map['start'] = time1;
    }
    if (!time2.contains("结束")) {
      map['end'] = time2;
    }
    map['title'] = order_num;
    map['p'] = page;
    map['per'] = 10;
    map['sign_type'] = SignType.USER_ORDER_LIST;
    Response<String> response = await HttpUtil().post(
        Api.BaseUrl + "/index.php/wap/UserOrder/getList",
        data: getSign(map));
    if (response.data == null) {
      return;
    }
    Map userMap = json.decode(response.data);
    if (userMap['code'] != 1) {
      Fluttertoast.showToast(msg: userMap['msg']);
      return;
    }
    print(response);
    OrderList3Entity homeListEntity = new OrderList3Entity.fromJson(userMap);
    if (mounted)
      setState(() {
        if (page == 1) {
          dataList.clear();
          dataList.addAll(homeListEntity.data.datas);
        } else {
          dataList.addAll(homeListEntity.data.datas);
        }
        if (page == 1) {
          _controller.resetLoadState();
          _controller.finishRefresh();
        } else {
          _controller.finishLoad(noMore: homeListEntity.data.datas.length == 0);
          if (homeListEntity.data.datas.length == 0) {
            Fluttertoast.showToast(msg: "暂无更多数据");
          }
        }
      });
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
    // TODO: implement build
    return new Container(
      child: new Container(
        color: divideColor,
        child: new Column(
          children: <Widget>[
            Expanded(
                child: new EasyRefresh(
              controller: _controller,
              header: MaterialHeader(),
              footer: MaterialFooter(),
              enableControlFinishLoad: true,
              enableControlFinishRefresh: true,
              child: ListView.separated(
                  itemBuilder: _renderRow,
                  separatorBuilder: (BuildContext context, int index) {
                    return Container(
                      color: divideColor,
                      height: 10,
                    );
                  },
                  itemCount: dataList == null ? 0 : dataList.length),
              onRefresh: () async {
                page = 1;
                loadList();
              },
              onLoad: () async {
                page++;
                loadList();
              },
            )),
          ],
        ),
      ),
    );
  }

  Widget _renderRow(BuildContext context, int index) {
    return new GestureDetector(
      onTap: () {
        Navigator.of(context).push(MaterialPageRoute(builder: (context) {
          return new OrderDetailPage(
            order_id: dataList[index].id,
            type: "2",
          );
        }));
      },
      child: new Container(
        decoration: BoxDecoration(
            color: Colors.white, borderRadius: BorderRadius.circular(0.0)),
        padding: EdgeInsets.all(ScreenUtil.getInstance().setHeight(DP_20)),
        child: new Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            // Text("下单时间: " + dataList[index].create_time,
            //     style: TextStyle(
            //         fontSize:
            //             ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE - 3))),
            Row(
              children: <Widget>[
                Expanded(
                  child: GestureDetector(
                      onTap: () {
                        ClipboardData data =
                            new ClipboardData(text: dataList[index].order_num);
                        Clipboard.setData(data);
                        Fluttertoast.showToast(
                            msg: "复制成功", gravity: ToastGravity.CENTER);
                      },
                      behavior: HitTestBehavior.opaque,
                      child: Row(children: <Widget>[
                        Text("订单号: " + dataList[index].order_num,
                            style: TextStyle(
                                fontSize: ScreenUtil.getInstance()
                                    .setSp(SMALL_FONT_SIZE))),
                        Icon(Icons.content_copy, size: 15)
                      ])),
                ),
                Text(dataList[index].status=='1'?'待支付':dataList[index].status=='2'?'待发货':dataList[index].status=='3'?'待收货':'已完成',
                    style: TextStyle(
                        color: mainColor,
                        fontSize:
                            ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE)))
              ],
            ),
            Container(
              height: 10,
            ),
            Container(
              child: Row(
                children: <Widget>[
                  ClipRRect(
                    borderRadius: BorderRadius.circular(2),
                    child: dataList[index].img == null|| dataList[index].img=='null'
                        ? Image.asset('images/app_icon.png',width: ScreenUtil.getInstance().setHeight(DP_120),
                            fit: BoxFit.fill,
                            height: ScreenUtil.getInstance().setHeight(DP_120))
                        : Image.network(Api.ImgBaseUrl + dataList[index].img,
                            width: ScreenUtil.getInstance().setHeight(DP_120),
                            fit: BoxFit.fill,
                            height: ScreenUtil.getInstance().setHeight(DP_120)),
                  ),
                  Container(
                    width: 10,
                  ),
                  Expanded(
                      child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Text(dataList[index].goods_name,
                          maxLines: 2,
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(
                              fontSize: ScreenUtil.getInstance()
                                  .setSp(NORMAL_FONT_SIZE))),
                      Container(
                        height: 4,
                      ),
                      Row(
                        children: <Widget>[
                          Text(MONEY + dataList[index].allprice,
                              style: TextStyle(
                                  color: mainColor,
                                  fontSize: ScreenUtil.getInstance()
                                      .setSp(NORMAL_FONT_SIZE))),
                          Container(
                            width: 20,
                          ),
                        ],
                      )
                    ],
                  ))
                ],
              ),
            ),
            Container(
              height: 10,
            ),
            Text(
                "收货人: " +
                    dataList[index].consignee.toString() +
                    "   " +
                    dataList[index].contact_number.toString(),
                style: TextStyle(
                    color: black_99,
                    fontSize: ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE))),
            Container(
              width: double.infinity,
              alignment: Alignment.centerRight,
              child: Text(
                  "实际付款: " +
                      (double.parse(dataList[index].allprice)).toString(),
                  style: TextStyle(
                      color: black,
                      fontSize:
                          ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE))),
            ),
            // Offstage(
            //   offstage: dataList[index].status != "7" ||
            //       dataList[index].refund_express_number != null,
            //   child: Text("退货地址: " + dataList[index].refund_address.toString(),
            //       style: TextStyle(
            //           color: black_99,
            //           fontSize: ScreenUtil.getInstance()
            //               .setSp(NORMAL_FONT_SIZE - 3))),
            // ),
            // Offstage(
            //   offstage: dataList[index].status != "11" ||
            //       dataList[index].refund_express_number != null,
            //   child: Text("换货地址: " + dataList[index].refund_address.toString(),
            //       style: TextStyle(
            //           color: black_99,
            //           fontSize: ScreenUtil.getInstance()
            //               .setSp(NORMAL_FONT_SIZE - 3))),
            // ),
            // Offstage(
            //   offstage: dataList[index].status != '8',
            //   child: Text(
            //       "拒绝理由: " + dataList[index].drawback_refuse_reason.toString(),
            //       style: TextStyle(
            //           color: mainColor,
            //           fontSize:
            //               ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE))),
            // ),
            // Offstage(
            //   offstage: dataList[index].status != '12',
            //   child: Text(
            //       "拒绝理由: " + dataList[index].drawback_refuse_reason.toString(),
            //       style: TextStyle(
            //           color: mainColor,
            //           fontSize:
            //               ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE))),
            // ),
            Container(
              height: 10,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.end,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Offstage(
                  offstage: dataList[index].status != "3",
                  child: GestureDetector(
                    behavior: HitTestBehavior.opaque,
                    child: Column(
                      children: <Widget>[
                        Image.asset("images/kf.png",
                            width: ScreenUtil.getInstance().setHeight(30)),
                        Text('客服',
                            style: TextStyle(
                                color: mainColor,
                                fontSize: ScreenUtil.getInstance().setSp(20)))
                      ],
                    ),
                    onTap: () {
                      if (APPLIC.userInfoMap == null) {
                        Fluttertoast.showToast(
                            msg: "请先登录", gravity: ToastGravity.CENTER);
                        Navigator.of(context)
                            .push(new MaterialPageRoute(builder: (context) {
                          return new WelComePage();
                        }));
                        return;
                      }
                      Navigator.of(context)
                          .push(new MaterialPageRoute(builder: (context) {
                        return KeFuTalkPage(
                            url:
                                "http://kf.0731app.cn/index/index/home?visiter_id=" +
                                    APPLIC.userInfoMap['user_msg']['uid']
                                        .toString() +
                                    "&visiter_name=" +
                                    "name" +
                                    "&avatar=" +
                                    APPLIC.userInfoMap['user_detail']['avatar']
                                        .toString() +
                                    "&business_id=1&groupid=4&special=5");
                      }));
                    },
                  ),
                ),
                Expanded(child: Center()),
                Offstage(
                  offstage: dataList[index].status != "1",
                  child: GestureDetector(
                    child: Container(
                      padding: EdgeInsets.fromLTRB(10, 2, 10, 2),
                      decoration: BoxDecoration(
                          border: Border.all(color: black_99, width: 1),
                          borderRadius: BorderRadius.circular(30)),
                      child: Text('取消订单',
                          style: TextStyle(
                              fontSize: ScreenUtil.getInstance()
                                  .setSp(SMALL_FONT_SIZE),
                              color: black_99)),
                    ),
                    onTap: () {
                      showDialog(
                        context: context,
                        builder: (context) => new AlertDialog(
                          title: new Text('提示'),
                          content: new Text('确定取消该条订单吗，取消后不可撤回'),
                          actions: <Widget>[
                            new FlatButton(
                              onPressed: () => Navigator.of(context).pop(false),
                              child: new Text(
                                '取消',
                                style: TextStyle(color: Colors.grey),
                              ),
                            ),
                            new FlatButton(
                              onPressed: () {
                                Navigator.of(context).pop(false);
                                cancle(index);
                              },
                              child: new Text('确定取消',
                                  style: TextStyle(color: mainColor)),
                            ),
                          ],
                        ),
                      );
                    },
                  ),
                ),
                Offstage(
                  offstage: dataList[index].status != "3",
                  child: GestureDetector(
                    child: Container(
                      margin: EdgeInsets.only(right: 10),
                      padding: EdgeInsets.fromLTRB(10, 2, 10, 2),
                      decoration: BoxDecoration(
                          border: Border.all(color: mainColor, width: 1),
                          borderRadius: BorderRadius.circular(30)),
                      child: Text('申请退货',
                          style: TextStyle(
                              fontSize: ScreenUtil.getInstance()
                                  .setSp(SMALL_FONT_SIZE),
                              color: mainColor)),
                    ),
                    onTap: () {
                      showCupertinoDialog(
                          context: context,
                          builder: (context) {
                            return CupertinoAlertDialog(
                              title: Text('请输入退货原因'),
                              content: Card(
                                elevation: 0.0,
                                child: Column(
                                  children: <Widget>[
                                    TextField(
                                      onChanged: (value) {
                                        backReason = value;
                                      },
                                      maxLines: 4,
                                      minLines: 4,
                                      cursorColor: mainColor,
                                      decoration: InputDecoration(
                                          hintText: '输入原因',
                                          border: InputBorder.none,
                                          filled: true,
                                          fillColor: Colors.grey.shade50),
                                    ),
                                  ],
                                ),
                              ),
                              actions: <Widget>[
                                CupertinoDialogAction(
                                  onPressed: () {
                                    Navigator.pop(context);
                                  },
                                  child: Text('取消',
                                      style: TextStyle(color: black_99)),
                                ),
                                CupertinoDialogAction(
                                  onPressed: () {
                                    Navigator.pop(context);
                                    // editName();
                                    submit(index);
                                  },
                                  child: Text('确定',
                                      style: TextStyle(color: mainColor)),
                                ),
                              ],
                            );
                          });
                      // Fluttertoast.showToast(msg: "敬请期待");
                    },
                  ),
                ),
                // Offstage(
                //   offstage: dataList[index].status != "3",
                //   child: GestureDetector(
                //     child: Container(
                //       margin: EdgeInsets.only(right: 10),
                //       padding: EdgeInsets.fromLTRB(10, 2, 10, 2),
                //       decoration: BoxDecoration(
                //           border: Border.all(color: mainColor, width: 1),
                //           // color: mainColor,
                //           borderRadius: BorderRadius.circular(30)),
                //       child: Text('申请换货',
                //           style: TextStyle(
                //               fontSize: ScreenUtil.getInstance()
                //                   .setSp(SMALL_FONT_SIZE),
                //               color: mainColor)),
                //     ),
                //     onTap: () {
                //       showCupertinoDialog(
                //           context: context,
                //           builder: (context) {
                //             return CupertinoAlertDialog(
                //               title: Text('请输入想换的商品规格'),
                //               content: Card(
                //                 elevation: 0.0,
                //                 child: Column(
                //                   children: <Widget>[
                //                     TextField(
                //                       onChanged: (value) {
                //                         backReason2 = value;
                //                       },
                //                       maxLines: 4,
                //                       minLines: 4,
                //                       cursorColor: mainColor,
                //                       decoration: InputDecoration(
                //                           hintText: '输入规格',
                //                           border: InputBorder.none,
                //                           filled: true,
                //                           fillColor: Colors.grey.shade50),
                //                     ),
                //                   ],
                //                 ),
                //               ),
                //               actions: <Widget>[
                //                 CupertinoDialogAction(
                //                   onPressed: () {
                //                     Navigator.pop(context);
                //                   },
                //                   child: Text('取消',
                //                       style: TextStyle(color: black_99)),
                //                 ),
                //                 CupertinoDialogAction(
                //                   onPressed: () {
                //                     Navigator.pop(context);
                //                     // editName();
                //                     submit2(index);
                //                   },
                //                   child: Text('确定',
                //                       style: TextStyle(color: mainColor)),
                //                 ),
                //               ],
                //             );
                //           });
                //       // Fluttertoast.showToast(msg: "敬请期待");
                //     },
                //   ),
                // ),
                // Offstage(
                //   offstage: dataList[index].status != "7" ||
                //       dataList[index].refund_express_number != null,
                //   child: GestureDetector(
                //     child: Container(
                //       margin: EdgeInsets.only(right: 10),
                //       padding: EdgeInsets.fromLTRB(10, 2, 10, 2),
                //       decoration: BoxDecoration(
                //           border: Border.all(color: mainColor, width: 1),
                //           borderRadius: BorderRadius.circular(30)),
                //       child: Text('填写退货快递单号',
                //           style: TextStyle(
                //               fontSize: ScreenUtil.getInstance()
                //                   .setSp(SMALL_FONT_SIZE),
                //               color: mainColor)),
                //     ),
                //     onTap: () {
                //       showCupertinoDialog(
                //           context: context,
                //           builder: (context) {
                //             return StatefulBuilder(builder: (context, myState) {
                //               myState1 = myState;
                //               return CupertinoAlertDialog(
                //                 title: Text('请输入快递单号'),
                //                 content: Card(
                //                   elevation: 0.0,
                //                   child: Column(
                //                     children: <Widget>[
                //                       GestureDetector(
                //                         onTap: () {
                //                           showCate();
                //                         },
                //                         child: Container(
                //                           color: white,
                //                           padding: EdgeInsets.all(10),
                //                           child: Row(
                //                             children: <Widget>[
                //                               Expanded(
                //                                 child: Text('$name',
                //                                     style: TextStyle(
                //                                         fontSize: 14,
                //                                         color: black)),
                //                               ),
                //                               Icon(Icons.arrow_forward_ios,
                //                                   size: 16)
                //                             ],
                //                           ),
                //                         ),
                //                       ),
                //                       TextField(
                //                         onChanged: (value) {
                //                           danhao = value;
                //                         },
                //                         maxLines: 1,
                //                         minLines: 1,
                //                         cursorColor: mainColor,
                //                         decoration: InputDecoration(
                //                             hintText: '输入快递单号',
                //                             border: InputBorder.none,
                //                             filled: true,
                //                             fillColor: Colors.grey.shade50),
                //                       ),
                //                     ],
                //                   ),
                //                 ),
                //                 actions: <Widget>[
                //                   CupertinoDialogAction(
                //                     onPressed: () {
                //                       Navigator.pop(context);
                //                     },
                //                     child: Text('取消',
                //                         style: TextStyle(color: black_99)),
                //                   ),
                //                   CupertinoDialogAction(
                //                     onPressed: () {
                //                       if (danhao == "") {
                //                         Fluttertoast.showToast(msg: "请输入快递单号");
                //                         return;
                //                       }
                //                       Navigator.pop(context);
                //                       // editName();
                //                       submitDanHao(index);
                //                     },
                //                     child: Text('确定',
                //                         style: TextStyle(color: mainColor)),
                //                   ),
                //                 ],
                //               );
                //             });
                //           });
                //       // Fluttertoast.showToast(msg: "敬请期待");
                //     },
                //   ),
                // ),
                // Offstage(
                //   offstage: dataList[index].status != "11" ||
                //       dataList[index].refund_express_number != null,
                //   child: GestureDetector(
                //     child: Container(
                //       margin: EdgeInsets.only(right: 10),
                //       padding: EdgeInsets.fromLTRB(10, 2, 10, 2),
                //       decoration: BoxDecoration(
                //           border: Border.all(color: mainColor, width: 1),
                //           borderRadius: BorderRadius.circular(30)),
                //       child: Text('填写换货快递单号',
                //           style: TextStyle(
                //               fontSize: ScreenUtil.getInstance()
                //                   .setSp(SMALL_FONT_SIZE),
                //               color: mainColor)),
                //     ),
                //     onTap: () {
                //       showCupertinoDialog(
                //           context: context,
                //           builder: (context) {
                //             return StatefulBuilder(builder: (context, myState) {
                //               myState1 = myState;
                //               return CupertinoAlertDialog(
                //                 title: Text('请输入快递单号'),
                //                 content: Card(
                //                   elevation: 0.0,
                //                   child: Column(
                //                     children: <Widget>[
                //                       GestureDetector(
                //                         onTap: () {
                //                           showCate();
                //                         },
                //                         child: Container(
                //                           color: white,
                //                           padding: EdgeInsets.all(10),
                //                           child: Row(
                //                             children: <Widget>[
                //                               Expanded(
                //                                 child: Text('$name',
                //                                     style: TextStyle(
                //                                         fontSize: 14,
                //                                         color: black)),
                //                               ),
                //                               Icon(Icons.arrow_forward_ios,
                //                                   size: 16)
                //                             ],
                //                           ),
                //                         ),
                //                       ),
                //                       TextField(
                //                         onChanged: (value) {
                //                           danhao = value;
                //                         },
                //                         maxLines: 1,
                //                         minLines: 1,
                //                         cursorColor: mainColor,
                //                         decoration: InputDecoration(
                //                             hintText: '输入快递单号',
                //                             border: InputBorder.none,
                //                             filled: true,
                //                             fillColor: Colors.grey.shade50),
                //                       ),
                //                     ],
                //                   ),
                //                 ),
                //                 actions: <Widget>[
                //                   CupertinoDialogAction(
                //                     onPressed: () {
                //                       Navigator.pop(context);
                //                     },
                //                     child: Text('取消',
                //                         style: TextStyle(color: black_99)),
                //                   ),
                //                   CupertinoDialogAction(
                //                     onPressed: () {
                //                       if (danhao == "") {
                //                         Fluttertoast.showToast(msg: "请输入快递单号");
                //                         return;
                //                       }
                //                       Navigator.pop(context);
                //                       // editName();
                //                       submitDanHao(index);
                //                     },
                //                     child: Text('确定',
                //                         style: TextStyle(color: mainColor)),
                //                   ),
                //                 ],
                //               );
                //             });
                //           });
                //       // Fluttertoast.showToast(msg: "敬请期待");
                //     },
                //   ),
                // ),
                Offstage(
                  offstage: dataList[index].status != "1",
                  child: GestureDetector(
                    child: Container(
                      padding: EdgeInsets.fromLTRB(10, 2, 10, 2),
                      margin: EdgeInsets.only(left: 10),
                      decoration: BoxDecoration(
                          color: mainColor,
                          border: Border.all(color: mainColor, width: 1),
                          borderRadius: BorderRadius.circular(30)),
                      child: Text('立即支付',
                          style: TextStyle(
                              fontSize: ScreenUtil.getInstance()
                                  .setSp(SMALL_FONT_SIZE),
                              color: white)),
                    ),
                    onTap: () {
                      Navigator.of(context)
                          .push<int>(MaterialPageRoute(builder: (context) {
                        return new OrderPayPage(
                            orderId: dataList[index].id,
                            create_time: dataList[index].create_time,
                            goods_id: dataList[index].goods_id,
                            orderNum: dataList[index].order_num,
                            totalMoney: formatNum(
                                (double.parse(dataList[index].allprice)), 2));
                      })).then((int result) {
                        if (result != null) {
                          dataList.removeAt(index);
                          setState(() {});
                        }
                        print("payresult===>$result");
                      });
                    },
                  ),
                ),
                Offstage(
                  offstage: dataList[index].status != "3",
                  child: GestureDetector(
                    child: Container(
                      padding: EdgeInsets.fromLTRB(10, 2, 10, 2),
                      decoration: BoxDecoration(
                          color: mainColor,
                          border: Border.all(color: mainColor, width: 1),
                          borderRadius: BorderRadius.circular(30)),
                      child: Text('确认收货',
                          style: TextStyle(
                              fontSize: ScreenUtil.getInstance()
                                  .setSp(SMALL_FONT_SIZE),
                              color: white)),
                    ),
                    onTap: () {
                      showDialog(
                        context: context,
                        builder: (context) => new AlertDialog(
                          title: new Text('提示'),
                          content: new Text('确定收货吗？'),
                          actions: <Widget>[
                            new FlatButton(
                              onPressed: () => Navigator.of(context).pop(false),
                              child: new Text(
                                '取消',
                                style: TextStyle(color: Colors.grey),
                              ),
                            ),
                            new FlatButton(
                              onPressed: () {
                                Navigator.of(context).pop(false);
                                confirm(index);
                              },
                              child: new Text('确定',
                                  style: TextStyle(color: mainColor)),
                            ),
                          ],
                        ),
                      );
                    },
                  ),
                ),
                Offstage(
                  offstage: dataList[index].logistics.replaceAll("null", "") == ""|| dataList[index].express_number.replaceAll("null", "")=="",
                  child: GestureDetector(
                    child: Container(
                      padding: EdgeInsets.fromLTRB(10, 2, 10, 2),
                      margin: EdgeInsets.fromLTRB(10, 0, 0, 0),
                      decoration: BoxDecoration(
                          color: mainColor,
                          border: Border.all(color: mainColor, width: 1),
                          borderRadius: BorderRadius.circular(30)),
                      child: Text(
                          dataList[index].status == '11' ? '换货物流' : '查看物流',
                          style: TextStyle(
                              fontSize: ScreenUtil.getInstance()
                                  .setSp(SMALL_FONT_SIZE),
                              color: white)),
                    ),
                    onTap: () {
                      Navigator.of(context)
                          .push(new MaterialPageRoute(builder: (context) {
                        return new MyHomePage(
                          title: dataList[index].logistics,
                          express_number:  dataList[index].express_number,
                          phone: dataList[index].contact_number,
                        );
                      }));
                    },
                  ),
                )
              ],
            )
          ],
        ),
      ),
    );
  }

  String formatNum(double num, int postion) {
    if ((num.toString().length - num.toString().lastIndexOf(".") - 1) <
        postion) {
      return num.toStringAsFixed(postion)
          .substring(0, num.toString().lastIndexOf(".") + postion + 1)
          .toString();
    } else {
      return num.toString()
          .substring(0, num.toString().lastIndexOf(".") + postion + 1)
          .toString();
    }
  }

  del(id) async {
    FormData formData = new FormData.fromMap({
      "token": token,
      "id": id,
    });
    Response<String> response =
        await HttpUtil().post(Api.DEL_ORDER, data: formData);
    Map userMap = json.decode(response.data);
    int code = userMap["code"];
    String msg = userMap["msg"];
    Navigator.of(context).pop(1);
    Fluttertoast.showToast(msg: msg);
    if (code == 0) {
      page = 1;
      loadList();
    }
    return true;
  }

  submitDanHao(int index) async {
    // Map map = new Map();
    // map['token'] = token;
    // map['order_id'] = dataList[index].id;
    // map['refund_express_number'] = danhao;
    // map['sign_type'] = SignType.FILL_NUM;
    // map['logistics_refund'] = bank_id;
    // Response<String> response =
    //     await HttpUtil().post(Api.FILL_NUM, data: getSign(map));
    // Map userMap = json.decode(response.data);
    // int code = userMap["code"];
    // String msg = userMap["msg"];
    // Fluttertoast.showToast(msg: msg);
    // if (code == 1 && mounted) {
    //   // dataList[index](index);
    //   dataList[index].refund_express_number = danhao;
    //   setState(() {});
    // }
  }

  submit(int index) async {
    Map map = new Map();
    map['token'] = token;
    map['order_id'] = dataList[index].id;
    map['drawback_reason'] = backReason;
    map['sign_type'] = SignType.APPLY_DRAWBACK;
    Response<String> response =
        await HttpUtil().post(Api.APPLY_DRAWBACK, data: getSign(map));
    Map userMap = json.decode(response.data);
    int code = userMap["code"];
    String msg = userMap["msg"];
    Fluttertoast.showToast(msg: msg);
    if (code == 1 && mounted) {
      dataList.removeAt(index);
      setState(() {});
      Navigator.of(context).push(new MaterialPageRoute(builder: (context) {
        return new Order2Page();
      }));
    }
  }

  submit2(int index) async {
    Map map = new Map();
    map['token'] = token;
    map['order_id'] = dataList[index].id;
    map['drawback_reason'] = backReason2;
    map['sign_type'] = SignType.HUANHUO;
    Response<String> response =
        await HttpUtil().post(Api.HUANHUO, data: getSign(map));
    Map userMap = json.decode(response.data);
    int code = userMap["code"];
    String msg = userMap["msg"];
    Fluttertoast.showToast(msg: msg);
    if (code == 1 && mounted) {
      dataList.removeAt(index);
      setState(() {});
      Navigator.of(context).push(new MaterialPageRoute(builder: (context) {
        return new Order2Page();
      }));
    }
  }

  confirm(int index) async {
    Map map = new Map();
    map['token'] = token;
    map['order_id'] = dataList[index].id;
    map['sign_type'] = SignType.CONFIRM_ORDER;
    Response<String> response = await HttpUtil().post(
        Api.BaseUrl + '/index.php/wap/UserOrder/confirm',
        data: getSign(map));
    Map userMap = json.decode(response.data);
    int code = userMap["code"];
    String msg = userMap["msg"];
    Fluttertoast.showToast(msg: msg);
    if (code == 1 && mounted) {
      dataList.removeAt(index);
      setState(() {});
    }
    return true;
  }

  cancle(int index) async {
    Map map = new Map();
    map['token'] = token;
    map['order_id'] = dataList[index].id;
    map['sign_type'] = SignType.CANCLE_ORDER;
    Response<String> response = await HttpUtil().post(
        Api.BaseUrl + "/index.php/wap/UserOrder/cancel",
        data: getSign(map));
    Map userMap = json.decode(response.data);
    int code = userMap["code"];
    String msg = userMap["msg"];
    Fluttertoast.showToast(msg: msg);
    if (code == 1 && mounted) {
      dataList.removeAt(index);
      setState(() {});
    }
    return true;
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
